<?php
// +----------------------------------------------------------------------
// | ManniuRead
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://manniuyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: heitao <read@manniuyun.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use manniu\controller\AdminBase;

use think\Session;
use app\admin\model\Admin as Admin;

class Login extends AdminBase{
	/**
     * 管理员登录
     * @access public
     * @return \think\Response
     */
    public function index(){
    	if($this->aid){
			$this->redirect('index/index');
		}
		if($this->request->isPost()){
			$u = $this->request->param('username');
			$p = $this->request->param('password');
			$admin = Admin::get(['username' => $u]);
			if(!$admin){
				$error['msg'] = '用户名或密码不正确';
			}else{
				$aid = (int)$admin->admin_login($admin, $p);
				if ($aid > 0) {
					$this->success('欢迎回来', 'index/index');
				}
				if ($aid == -2) {
	                $error['msg'] = '用户名或密码不正确';
	            } elseif ($aid == -3) {
	                $error['msg'] = '此管理员无角色组';
	            } elseif ($aid == -4) {
	                $error['msg'] = '此管理员角色组被禁用';
	            } else {
	                $error['msg'] = '未知错误，请联系管理员';
	            }
			}
			$this->error($error['msg']);
			
		}
		// 临时关闭当前模板的布局功能
		$this->view->engine->layout(false);
     	return $this->fetch();
    }

    /**
     * 退出
     * @access public
     * @return void
     */
	public function logout(){
		Session::clear();
		$this->success('已安全退出','login/index');
		/*
		if($this->checkLogin()){
			Session::clear();
			$this->success('已安全退出','login/index');
		}else{
			$this->redirect('index');
		}
		*/
	}
}
